package com.bestom.bestoma10demo;

import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author bestom
 * set Date and Time
 */
public class DateVTime {

    private static DateVTime instance = null;

    public static DateVTime getInstance() {
        if (instance == null) instance = new DateVTime();
        return instance;
    }

    /**set Time*/
    public void setTime(Context context, int hourOfDay, int minute) {
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
        mCalendar.set(Calendar.MINUTE, minute);
        mCalendar.set(Calendar.SECOND, 0);
        mCalendar.set(Calendar.MILLISECOND, 0);
        long when = mCalendar.getTimeInMillis();
        if (when / 1000 < Integer.MAX_VALUE) {
            ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);
        }
    }

    /**set Date*/
    public void setDate(Context context, int year, int month, int day) {
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.set(Calendar.YEAR, year);
        mCalendar.set(Calendar.MONTH, month);
        mCalendar.set(Calendar.DAY_OF_MONTH, day);
        long when = mCalendar.getTimeInMillis();
        if (when / 1000 < Integer.MAX_VALUE) {
            ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);
        }
    }

    /**set time format*/
    public String transform(String pattern, long msec) {
        return new SimpleDateFormat(pattern).format(msec);
    }

    public void setClock(final Context mContext){
        final AlarmManager mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        Calendar mCalendar = Calendar.getInstance();
        Dialog dialog = new TimePickerDialog(mContext,
                (timePicker, hourOfDay, minute) -> {
                    // Get the date object
                    Calendar c=Calendar.getInstance();
                    c.setTimeInMillis(System.currentTimeMillis());
                    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    c.set(Calendar.MINUTE, minute);
                    c.set(Calendar.SECOND, 0);
                    c.set(Calendar.MILLISECOND, 0);

                    Intent intent = new Intent(mContext, AlarmReceiver.class);
                    PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

                    if(c.getTimeInMillis() < System.currentTimeMillis()){
                        mAlarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis()+24*60*60*1000, pi);
                    }else{
                        mAlarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
                    }
                    Intent intent1 = new Intent("up_view");
                    intent1.putExtra("up_view", "Set the alarm successfully and it will ring at " + hourOfDay + ":" + minute);
                    mContext.sendBroadcast(intent1);
                }, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE),
                true);
        dialog.show();
    }
}
